package com.wswy.wzcx.ui.weather

import android.graphics.Bitmap
import android.graphics.Canvas
import com.facebook.common.references.CloseableReference
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory
import com.facebook.imagepipeline.request.BasePostprocessor

class ProxyWeatherBgProcessor(private val w:Int,
                              private val h:Int) : BasePostprocessor() {

    private companion object {
        private const val TAG = "ProxyWeatherBgProcessor"
    }

    override fun process(sourceBitmap: Bitmap?, bitmapFactory: PlatformBitmapFactory?): CloseableReference<Bitmap>? {

//        val w = 968
//        val h = 812

        sourceBitmap?.run {

            val destBitmapRef = bitmapFactory?.createBitmapInternal(
                    w,
                    h,
                    config ?: FALLBACK_BITMAP_CONFIGURATION)
            try {

                val destBitmap = destBitmapRef?.get()

                val canvas = Canvas(destBitmap)

                val scale = w.toFloat() / width.toFloat()

                val newSrc =  Bitmap.createBitmap(this,0,0,(width*scale).toInt(),(height*scale).toInt())

                canvas.drawBitmap(Bitmap.createBitmap(newSrc,0,0,w,h),0f,0f,null)

                return CloseableReference.cloneOrNull(destBitmapRef)
            } catch (e:Throwable){
                e.printStackTrace()
            } finally {
                CloseableReference.closeSafely(destBitmapRef)
            }
        }

        return super.process(sourceBitmap, bitmapFactory)
    }


}